import { defineStore } from "pinia";
import router from "@/router";
import api from "@/api/index.js";
export const useRouterStore = defineStore("router", {
   state: () => ({
      routerList: [
         {
            path: "/platformManagement/home",
            title: "首页",
            icon: "Bicycle",
            type: "menu",
            disabled: true,
            // children: [

            //    // {
            //    //    path: "/platformManagement/home",
            //    //    title: "首页",
            //    //    icon: "HomeFilled",
            //    //    type: "menu",
            //    //    disabled: true,
            //    // },

            //    // {
            //    //    path: "/platformManagement/parking",
            //    //    title: "车位管理",
            //    //    icon: "Guide",
            //    //    type: "menu",
            //    //    disabled: true,
            //    //    children: [
            //    //       {
            //    //          path: "/platformManagement/parking/store",
            //    //          title: "管家站车位管理",
            //    //          type: "menu",
            //    //          disabled: true,
            //    //       },
            //    //    ],
            //    // },
            //    // {
            //    //    path: "/platformManagement/marketingManagement",
            //    //    title: "营销管理",
            //    //    icon: "Shop",
            //    //    type: "menu",
            //    //    disabled: true,
            //    //    children: [
            //    //       {
            //    //          path: "/platformManagement/marketingManagement/marketingActivities",
            //    //          title: "营销活动",
            //    //          type: "menu",
            //    //          disabled: true,
            //    //       },
            //    //       {
            //    //          path: "/platformManagement/lottery/lottery",
            //    //          title: "抽奖活动",
            //    //          type: "menu",
            //    //          disabled: true,
            //    //       },
            //    //       {
            //    //          path: "/platformManagement/keyCabinet",
            //    //          title: "抽奖柜管理",
            //    //          type: "menu",
            //    //          disabled: true,
            //    //       },
            //    //    ],
            //    // },

            // ],
         },

         {
            path: "/procurementManagement",
            title: "采购管理",
            icon: "Bicycle",
            type: "menu",
            disabled: true,
            children: [
               // {
               //    path: "/platformManagement/procurementManagement/procurementOverview",
               //    title: "采购总览",
               //    icon: "Connection",
               //    type: "menu",
               //    disabled: true,
               // },
               {
                  path: "/platformManagement/procurementManagement/supplierManagement",
                  title: "供应商管理",
                  icon: "DocumentAdd",
                  type: "menu",
                  disabled: true,
               },
               {
                  path: "/platformManagement/procurementManagement/purchaseOrderManagement",
                  title: "采购订单管理",
                  icon: "SetUp",
                  type: "menu",
                  disabled: true,
               },
               {
                  path: "/platformManagement/procurementManagement/purchaseOrderReview",
                  title: "采购订单审核",
                  icon: "Folder",
                  type: "menu",
                  disabled: true,
               },
               {
                  path: "/platformManagement/procurementManagement/purchaseReturn",
                  title: "采购退货",
                  icon: "Reading",
                  type: "menu",
                  disabled: true,
               },
               {
                  path: "/platformManagement/procurementManagement/purchaseReturnReview",
                  title: "采购退货审核",
                  icon: "DataLine",
                  type: "menu",
                  disabled: true,
               },
               {
                  path: "/platformManagement/procurementManagement/purchaseReport",
                  title: "采购报表",
                  icon: "",
                  type: "menu",
                  disabled: true,
                  children: [
                     {
                        path: "/platformManagement/procurementManagement/purchaseReport/purchaseDetails",
                        title: "采购明细",
                        type: "menu",
                        disabled: true,
                     },
                     {
                        path: "/platformManagement/procurementManagement/purchaseReport/procurementStatistics_products",
                        title: "采购统计（按产品）",
                        type: "menu",
                        disabled: true,
                     },
                     {
                        path: "/platformManagement/procurementManagement/purchaseReport/procurementStatistics_suppliers",
                        title: "采购统计（按供应商）",
                        type: "menu",
                        disabled: true,
                     },

                     {
                        path: "/platformManagement/procurementManagement/purchaseReport/purchaseReturnDetails",
                        title: "采购退货明细",
                        type: "menu",
                        disabled: true,
                     },
                     {
                        path: "/platformManagement/procurementManagement/purchaseReport/returnStatistics_products",
                        title: "采购退货统计（按产品）",
                        type: "menu",
                        disabled: true,
                     },
                     {
                        path: "/platformManagement/procurementManagement/purchaseReport/returnStatistics_suppliers",
                        title: "采购退货统计（按供应商）",
                        type: "menu",
                        disabled: true,
                     },
                  ]
               },
            ],
         },
         {
            path: "/platformManagement/productManagement/product",
            title: "产品管理",
            icon: "Bicycle",
            type: "menu",
            disabled: true,

            // children: [
            //    {
            //       path: "/platformManagement/productManagement/product",
            //       title: "产品管理",
            //       type: "menu",
            //       disabled: true,
            //    },
            // ],
         },
         {
            path: "/platformManagement/Outbound",
            title: "补库管理",
            icon: "Bicycle",
            type: "menu",
            disabled: true,
            children: [
               // {
               //    path: "/platformManagement/Outbound/Outbound",
               //    title: "补库总览",
               //    type: "menu",
               //    disabled: true,
               // },
               {
                  path: "/platformManagement/Outbound/OutboundManagement",
                  title: "客户管理",
                  type: "menu",
                  disabled: true,
               },
               {
                  path: "/platformManagement/Outbound/OutboundOrder",
                  title: "补库订单管理",
                  type: "menu",
                  disabled: true,
               },
               {
                  path: "/platformManagement/Outbound/OutboundAudit",
                  title: "补库订单审核",
                  type: "menu",
                  disabled: true,
               },
               {
                  path: "/platformManagement/Outbound/OutboundReturnManagement",
                  title: "补库退货",
                  type: "menu",
                  disabled: true,
               },
               {
                  path: "/platformManagement/Outbound/OutboundReturnReview",
                  title: "补库退货审核",
                  type: "menu",
                  disabled: true,
               },
               // {
               //    path: "/platformManagement/Outbound/OutboundReport",
               //    title: "补库报表",
               //    type: "menu",
               //    disabled: true,
               //    children: [
               //       {
               //          path: "/platformManagement/Outbound/OutboundReport/OutboundDetails",
               //          title: "补库明细",
               //          type: "menu",
               //          disabled: true,
               //       },
               //       {
               //          path: "/platformManagement/Outbound/OutboundReport/OutboundStatisticsList",
               //          title: "补库统计",
               //          type: "menu",
               //          disabled: true,
               //       },
               //       {
               //          path: "/platformManagement/Outbound/OutboundReport/ReturnDetails",
               //          title: "补库退货明细",
               //          type: "menu",
               //          disabled: true,
               //       },
               //       {
               //          path: "/platformManagement/Outbound/OutboundReport/ReturnStatistics",
               //          title: "补库退货统计",
               //          type: "menu",
               //          disabled: true,
               //       },
               //    ],
               // },
            ],
         },
         {
            path: "/platformManagement/warehouseManagement/storageIndex",
            title: "仓储管理",
            icon: "Bicycle",
            type: "menu",
            disabled: true,
            children: [
               {
                  path: "/platformManagement/warehouseManagement/storageIndex",
                  title: "仓库管理",
                  type: "menu",
                  icon: "Bicycle",
                  disabled: true,
               },
               {
                  path: "/platformManagement/warehouseManagement/WarehouseInventory",
                  title: "产品库存",
                  type: "menu",
                  icon: "PictureRounded",
                  disabled: true,
               },
               {
                  path: "/platformManagement/warehouseManagement/productStorage",
                  title: "产品入库",
                  type: "menu",
                  icon: "ShoppingBag",
                  disabled: true,
                  children: [
                     {
                        path: "/platformManagement/warehouseManagement/productStorage",
                        title: "主库入库",
                        type: "menu",
                     },
                     {
                        path: "/platformManagement/warehouseManagement/productStorageSon",
                        title: "子库入库",
                        type: "menu",
                     },
                  ]
               },

               {
                  path: "/platformManagement/warehouseManagement/StorageRecord",
                  title: "入库记录",
                  type: "menu",
                  icon: "TrophyBase",
                  disabled: true,
               },

               {
                  path: "/platformManagement/warehouseManagement/ProductOutbound",
                  title: "产品出库",
                  type: "menu",
                  icon: "TakeawayBox",
                  disabled: true,
               },

               {
                  path: "/platformManagement/warehouseManagement/OutboundRecord",
                  title: "出库记录",
                  type: "menu",
                  icon: "ReadingLamp",
                  disabled: true,
               },



               {
                  path: "/platformManagement/warehouseManagement/InventoryCount",
                  title: "库存盘点",
                  type: "menu",
                  icon: "Coin",
                  disabled: true,
               },
               // {
               //    path: "/platformManagement/warehouseManagement/InventoryReview",
               //    title: "库存盘点审核",
               //    type: "menu",
               //    icon: "Mouse",
               //    disabled: true,
               // },
               {
                  path: "/platformManagement/warehouseManagement/InventoryTransfer",
                  title: "库存调拨",
                  type: "menu",
                  icon: "Help",
                  disabled: true,
               },
               {
                  path: "/platformManagement/warehouseManagement/TransferReview",
                  title: "库存调拨审核",
                  type: "menu",
                  icon: "MessageBox",
               },
               // {
               //    path: "/platformManagement/warehouseManagement",
               //    title: "仓储报表",
               //    type: "menu",
               //    icon: "Expand",
               //    disabled: true,
               //    children: [
               //       {
               //          path: "/platformManagement/warehouseManagement/InventoryStatistics",
               //          title: "产品库存统计",
               //          type: "menu",
               //          disabled: true,
               //       },
               //    ],
               // },
            ],
         },
         // {
         //    path: "/platformManagement/financialManagement",
         //    title: "财务管理",
         //    icon: "Bicycle",
         //    type: "menu",
         //    disabled: true,
         // children: [
         //    {
         //       path: "/platformManagement/financialManagement/orderPay",
         //       title: "订单付款",
         //       type: "menu",
         //       disabled: true,
         //    },
         //    {
         //       path: "/platformManagement/financialManagement/accountsPayable_others",
         //       title: "其他付款",
         //       type: "menu",
         //       disabled: true,
         //    },
         //    {
         //       path: "/platformManagement/financialManagement/paymentApproval",
         //       title: "付款审批",
         //       type: "menu",
         //       disabled: true,
         //    },
         //    {
         //       path: "/platformManagement/financialManagement/paymentRecord",
         //       title: "付款记录",
         //       type: "menu",
         //       disabled: true,
         //    },
         //    {
         //       path: "/platformManagement/financialManagement/orderPayment",
         //       title: "订单回款",
         //       type: "menu",
         //       disabled: true,
         //    },

         //    {
         //       path: "/platformManagement/financialManagement/receivableOther",
         //       title: "其他收款",
         //       type: "menu",
         //       disabled: true,
         //    },
         //    {
         //       path: "/platformManagement/financialManagement/collectionRecord",
         //       title: "收款记录",
         //       type: "menu",
         //       disabled: true,
         //    },
         //    {
         //       path: "/platformManagement/financialManagement/financialStatements",
         //       title: "财务报表",
         //       type: "menu",
         //       disabled: true,
         //       children: [
         //          {
         //             path: "/platformManagement/financialManagement/financialStatements/accountsReceivable",
         //             title: "应收账款",
         //             type: "menu",
         //             disabled: true,
         //          },
         //          {
         //             path: "/platformManagement/financialManagement/financialStatements/accountsPayable",
         //             title: "应付账款",
         //             type: "menu",
         //             disabled: true,
         //          },
         //          {
         //             path: "/platformManagement/financialManagement/financialStatements/supplierStatement",
         //             title: "供货商对账单",
         //             type: "menu",
         //             disabled: true,
         //          },
         //          {
         //             path: "/platformManagement/financialManagement/financialStatements/customerStatement",
         //             title: "客户对账单",
         //             type: "menu",
         //             disabled: true,
         //          },
         //       ],
         //    }
         // ],
         // },
         {
            path: "/platformManagement/financialManagement/basicData",
            title: "基础资料",
            type: "menu",
            disabled: true,
            children: [
               // {
               //    path: "/platformManagement/financialManagement/basicData/numberingRules",
               //    title: "编号规则",
               //    type: "menu",
               //    disabled: true,
               // },
               // {
               //    path: "/platformManagement/financialManagement/basicData/accountManagement",
               //    title: "账户管理",
               //    type: "menu",
               //    disabled: true,
               // },
               {
                  path: "/platformManagement/financialManagement/basicData/unitMeasure",
                  title: "计量单位",
                  type: "menu",
                  disabled: true,
               },
               {
                  path: "/platformManagement/financialManagement/basicData/customerLevel",
                  title: "客户等级",
                  type: "menu",
                  disabled: true,
               },
               {
                  path: "/platformManagement/financialManagement/basicData/supplierGrade",
                  title: "供应商等级",
                  type: "menu",
                  disabled: true,
               },
               {
                  path: "/platformManagement/financialManagement/basicData/productManagement",
                  title: "产品管理(SPU)",
                  type: "menu",
                  disabled: true,
               },
               {
                  path: "/platformManagement/financialManagement/basicData/consumablesSettings",
                  title: "耗材设置",
                  type: "menu",
                  disabled: true,
               },
               {
                  path: "/platformManagement/financialManagement/basicData/replenishmentRules",
                  title: "补货规则设置",
                  type: "menu",
                  disabled: true,
               },
               // {
               //    path: "/platformManagement/financialManagement/basicData/ProductAttributeSettings",
               //    title: "产品属性设置",
               //    type: "menu",
               //    disabled: true,
               // },
               // {
               //    path: "/platformManagement/financialManagement/basicData/attributeValueManagement",
               //    title: "属性值管理",
               //    type: "menu",
               //    disabled: true,
               // },
            ],
         },
         {
            path: "/platformManagement/systemSettings",
            title: "系统设置",
            icon: "Bicycle",
            type: "menu",
            disabled: true,
            children: [
               {
                  path: "/platformManagement/systemSettings/organizationalStructure",
                  title: "组织架构",
                  type: "menu",
                  disabled: true,
               },
               {
                  path: "/platformManagement/systemSettings/rolePermissions",
                  title: "角色权限",
                  type: "menu",
                  disabled: true,
               },
               // {
               //    path: "/platformManagement/systemSettings/personnelManagement",
               //    title: "人员管理",
               //    type: "menu",
               //    disabled: true,
               // },
               {
                  path: "/platformManagement/systemSettings/permissionUser",
                  title: "用户管理",
                  type: "menu",
                  disabled: true,
               },
               {
                  path: "/platformManagement/systemSettings/menuManagement",
                  title: "菜单管理",
                  type: "menu",
                  disabled: true,
               },
               // {
               //    path: "/platformManagement/systemSettings/userActionLog",
               //    title: "用户操作日志",
               //    type: "menu",
               //    disabled: true,
               // },
               // {
               //    path: "/platformManagement/systemSettings/loginLogs",
               //    title: "登录日志",
               //    type: "menu",
               //    disabled: true,
               // },

            ],
         },
      ],

      // 路由列表信息
      index: 0, // 当前路由路径
      children: [], // 当前路由的子路由
      leftWidth: "0px",
      isCollapse: false, // 左侧菜单是否收起
      menuList: [],
   }),
   actions: {
      upLoad(res, isJump = false, item) {
         this.children = res;
         const targetPath = this.children?.[0]?.path || this.children?.path || item?.path;

         if (isJump && targetPath) {
            router.push(targetPath);
         }
         this.updateLeftWidth();
      },

      toggleCollapse() {
         this.isCollapse = !this.isCollapse;
         this.updateLeftWidth();
      },

      updateLeftWidth() {
         this.leftWidth = this.children?.length > 0
            ? (this.isCollapse ? "64px" : "220px")
            : "0px";
      },

      async getmenuList(forceRefresh = false) {
         this.loading = true;
         this.error = null;
         try {
            const res = await api.permission.user_menu_list();
            if (res.code === 200) {
               return res.data.menu_paths || [];
            }
            throw new Error(res.message || 'Failed to get menu list');
         } catch (error) {
            this.error = error;
            console.error("API error:", error);
            return [];
         } finally {
            this.loading = false;
         }
      },
      // 过滤路由
      async filterMenuList() {
         const menuPaths = await this.getmenuList();
         console.log('用户权限路径:', menuPaths);

         if (!menuPaths?.length) {
            this.menuList = [];
            return [];
         }
         const filterRoutes = (routes) => {
            return routes.filter(route => {
               const hasPermission = menuPaths.includes(route.path);
               console.log(`路由 ${route.path} (${route.title}) 权限检查:`, hasPermission);

               if (route.children && route.children.length > 0) {
                  const filteredChildren = filterRoutes(route.children);
                  route.children = filteredChildren;
                  // 如果父级有权限或者有子级有权限，则显示
                  const shouldShow = hasPermission || filteredChildren.length > 0;
                  console.log(`父级菜单 ${route.title} 是否显示:`, shouldShow);
                  return shouldShow;
               }

               return hasPermission;
            });
         };
         let _r = []
         let dataArr = JSON.parse(JSON.stringify(this.routerList));//深拷贝
         // console.log(dataArr, "777");
         if (filterRoutes(dataArr).length > 0) {
            _r = filterRoutes(dataArr);
            // console.log(_r, "666")
            localStorage.setItem("routerList", JSON.stringify(_r));
            return _r;
         } else {
            return [];
         }
      }
   }
});

